<?php

namespace App\Http\Controllers\V1\Api;

use App\Http\Requests\UserRequest;
use App\Models\V1\Admins;
use App\Models\V1\Advert;
use App\Models\V1\Bank;
use App\Models\V1\BankClass;
use App\Models\V1\Game;
use App\Models\V1\SysPermission;
use App\Models\V1\SysRole;
use App\Models\V1\UpdateLog;
use App\Models\V1\UserPermission;
use App\Models\V1\Users;
use Earnp\GoogleAuthenticator\GoogleAuthenticator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class UserPermissionController extends BaseController
{


    /**
     * 用户权限详情-api
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function Info(Request $request)
    {
        $product = $request->input('product', "");
        $user_id = $request->input('user_id', "");
        $data = UserPermission::where('user_id', $user_id)->where('product', $product)->first();
        return ajax_output(200, "成功", $data);
    }

    /**
     * 修改(增加)用户权限-api
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $product = $request->input('product');
        $user_id = $request->input('user_id');
        $exists = UserPermission::where('user_id', $user_id)->where('product', $product)->exists();

        $data = [];
        if ($exists) {
            if ($request->filled('menu_permission')) {
                $data['menu_permission'] = $request['menu_permission'];
            }
            if ($request->filled('game_permission')) {
                $data['game_permission'] = $request['game_permission'];
            }
            UserPermission::where('user_id', $user_id)->where('product', $product)->update($data);
        } else {
            $model = new UserPermission();
            $model->create([
                'ulid' => time(),
                'product' => $request->input('product'),
                'user_id' => $request->input('user_id'),
                'pid' => $request->input('pid'),
                'menu_permission' => $request->input('menu_permission'),
                'game_permission' => $request->input('game_permission'),
            ]);
        }
        return ajax_output(200, "成功");
    }

    /**
     * 日志列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
//    public function List(Request $request)
//    {
//
//        $query = UpdateLog::query();
//
//        $pageSize = $request->input('pageSize', 10);
//        $order = $request->input('order', 'desc');
//        $sort = $request->input('sort', 'id');
//
//
//        $query->where('state', 1);
//        $query->orderBy($sort, $order);
//        $data = $query->paginate($pageSize);
//
//        return ajax_output(200, "成功", $data);
//    }


}
